package com.ruitu.health_2.ui.common;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.VideoModel;
import com.ruitu.health_2.mvp.presenter.VideoPresenter;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.product.Video;
import com.ruitu.router_module.util.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class VideoListActivity<P, M> extends BaseRefreshActivity<VideoPresenter.VideoListP, VideoModel.VideoListM> {
    protected RelativeLayout rl_empty;

    protected VideoListAdapter adapter;
    protected List<Video> videoList = new ArrayList<>();// 视频列表数据

    protected int currPage = 1;
    protected int totalPage = 1;
    protected String name = "";// 传过来的搜索关键字

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_video_list;
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("videoId", videoList.get(position).getId());
        bundle.putString("videoUrl", videoList.get(position).getVideo());
        startActivity(VideoDetailActivity.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl_empty = findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关视频");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            name = bundle.getString("name");
        }

        setHeadTitle("视频列表");

        reqDataList();
        showProgress();
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(videoList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }

    protected void reqDataList() {
//        p.reqVideoList(20, currPage);
        p.reqVideoListByTitle(20, currPage, name);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 这段代码暂时保留
            if (currPage == 1) {
                videoList.clear();
            }
            PageModel2<Video> videoPage = (PageModel2<Video>) data;
            totalPage = videoPage.getTotalPage();
            List<Video> newData = videoPage.getList();
            videoList.addAll(newData);
            adapter.setDataList(videoList);
        }
        if (code == 2) {// 根据名查询视频列表
            if (currPage == 1) {
                videoList.clear();
            }
            PageModel2<Video> videoPage = (PageModel2<Video>) data;
            totalPage = videoPage.getTotalPage();
            List<Video> newData = videoPage.getList();
            videoList.addAll(newData);
            adapter.setDataList(videoList);
        }

        showOrHideEmptyView();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new VideoListAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lrcv_list);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqDataList();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    public static class VideoListAdapter extends ListBaseAdapter<Video> {

        public VideoListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_video_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RoundedImageView icon = holder.getView(R.id.icon);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_art = holder.getView(R.id.tv_art);
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_subname = holder.getView(R.id.tv_subname);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
            params.width = params.height * 16 / 10;
            icon.setLayoutParams(params);

            Video v = mDataList.get(position);
            tv_name.setText(v.getTitle());
            tv_art.setText(v.getArtName());
//            tv_time.setText(v.getCreateTime());
            tv_subname.setText(v.getDetail());

            GlideUtil.loadImg(v.getImgUrl(), icon);
        }
    }
}
